package com.bhs.sansong.ui.mine;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.ListPrivateLinesAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CompanyInfo;
import com.bhs.sansong.bean.CompanyInformation;
import com.bhs.sansong.bean.ListPrivateLines;
import com.bhs.sansong.bean.PrivateLines;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectPhone2Dialog;
import com.bhs.sansong.ui.findLogistics.CompanyDetailsActivity;
import com.bhs.sansong.ui.shipments.IssueNewOrderActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 历史足迹
 */
public class HistoricalFootprintActivity extends BaseActivity implements NetCallBack {

    private TextView mTvTitle;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private ListPrivateLinesAdapter mAdapter;
    private int page = 1;

    @Override
    public int initLayout() {
        return R.layout.activity_historical_footprint;
    }

    @Override
    public void initView() {

        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("历史足迹");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new ListPrivateLinesAdapter(3);
        loadData();
    }

    private void loadData() {

        networkRequest(netApi.getLogisticsVisitList(), this);
    }


    @Override
    public void initData() {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CompanyInfo companyInfo = mAdapter.getData().get(position).getLogistics_info();
                startActivity(new Intent(mActivity, CompanyDetailsActivity.class)
                        .putExtra("logistics_id", companyInfo.getLogistics_id()));

            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PrivateLines data = mAdapter.getData().get(position);
                CompanyInfo companyInfo = mAdapter.getData().get(position).getLogistics_info();
                if (view.getId() == R.id.ll_contact) {
                    contactImmediately(data);
                } else if (view.getId() == R.id.tv_consent) {
                    CompanyInformation companyInformation = new CompanyInformation(companyInfo.getCompany_pic(),
                            companyInfo.getAddress(), String.valueOf(companyInfo.getLogistics_id()),
                            data.getLine_info().getLine_id(), companyInfo.getCompany_name());
                    companyInformation.setStartCity(data.getLine_info().getStart_city());
                    companyInformation.setEndCity(data.getLine_info().getEnd_city());
                    startActivity(new Intent(mActivity, IssueNewOrderActivity.class)
                            .putExtra("order_type", "1")
                            .putExtra("place_order_type", "1")
                            .putExtra("networkCity", data.getLine_info().getEnd_city())
                            .putExtra("companyInfo", companyInformation));
                }
            }
        });

    }

    BottomSelectPhone2Dialog bottomSelectDialog;
    BasePopupView basePopupView;

    private void contactImmediately(PrivateLines data) {
        if (basePopupView == null) {
            bottomSelectDialog = new BottomSelectPhone2Dialog(this, data.getLogistics_info().getMobile(), data.getLogistics_info().getFixed_telephone(), data.getLine_info().getLine_mobile());
            basePopupView = new XPopup.Builder(this)
                    .asCustom(bottomSelectDialog)
                    .show();
        } else {
            basePopupView.show();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            loadingPopupDismiss();
            ListPrivateLines data = new Gson().fromJson(json, ListPrivateLines.class);
            mRefreshLayout.setRefreshing(false);
            if (data.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(data.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(data.getData().getList());
                }
                if (mAdapter.getData().size() >= data.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}